import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import { Toast } from 'antd-mobile';
import Utils from '../../../utils/index'
import DB from '../../../app/db';
import nattyStorage from 'natty-storage';
import StorageUtils from '../../../app/storage'
import { Control } from 'react-keeper'

const storageUser = nattyStorage({
  type: 'localStorage',
  key: 'sskj-wechat',
});

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    courname:'',
    court:{},
    userInfo: {
      id:'',
      name:'',
      hoseName:'',
      state:'',
      doors:[],
      courts:[],
      priType:'',
      openId:'',
      certNum: '',
      phone: '',
      vcode: ''
    }
  },

  initMode: function () {
    let t = this;
    t.data.userInfo = update(t.data.userInfo, {courts: {$set: []}});
    t.data.court = update(t.data.court, {courts: {$set: {}}});

    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
  },

  onCouSearch: function (params) {
    let t = this;
    t.data.courname = update(t.data.courname,  {$set: ''});
    t.couFeach();
    t.updateComponent();
  },

  onCouSet: function (params) {
    let t = this;
    console.info('1',params.name);
    t.data.court = update(t.data.court, {courts: {$set: params}});
    this.props.onSelectCour(t.data.court);
    t.updateComponent();
  },


  onCouSearchChange: function (params) {
    let t = this;
    t.data.courname = update(t.data.courname,  {$set: params});

    t.couFeach();
    t.updateComponent();
  },


  couFeach: function () {
    let t = this;
    DB.WechatAPI.getCourtAll({
      name:t.data.courname,
      size:10,
      page:1
    }).then(
      function (content) {
        console.info(content);
        let resultObj = Utils.isEmptyObject(content);
        if (!resultObj.success) {
        }
        else
        {
          if (content.length <= 0) {
            console.info('未查找到数据');
          }
          else
          {
            console.info(content);
            t.data.userInfo = update(t.data.userInfo, {courts: {$set: content.content}});

          }
          console.info(t.data.userInfo);


          t.updateComponent();

        };

      },
      function (error) {
        console.log(error);
      });
  },


  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});

